package com.jeason.fileserver.controller;

import com.jeason.fileserver.domain.LoginForm;
import com.jeason.fileserver.domain.LoginUser;
import com.jeason.fileserver.entity.SysUser;
import com.jeason.fileserver.service.LoginService;
import com.jeason.fileserver.utils.AjaxResult;
import com.jeason.fileserver.utils.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: jeason
 * @Description:
 * @Date: 2021/1/17 19:14
 */
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginForm loginForm){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        LoginUser user = loginService.login(loginForm.getUsername(), loginForm.getPassword());
        String token = tokenService.createToken(user);

        ajax.put("token", token);
        ajax.put("userInfo", user);
        return ajax;
    }

}
